% MATLAB Code for Investor Decision Tree Flowchart

% Define Unique Nodes
nodes = { ...
    'Start_Uncertainty', ...
    'Extreme_Uncertainty', ...
    'Mild_Uncertainty', ...
    'Strong_Prior_Beliefs', ...
    'Prob_Weighting_Dominates', ...
    'Extrapolative_Beliefs_Dominate', ...
    'Underreact_Denial', ...
    'Overreact_Panic', ...
    'Overreact_Herding', ...
    'Underreact_Delayed', ...
    'Risk_Sensitive', ...
    'Mild_Prob_Weighting', ...
    'Mild_Extrapolative', ...
    'Underreact_Cautious', ...
    'Overreact_Hedging', ...
    'Overreact_Momentum', ...
    'Underreact_Slow'
};

% Define Edges
edges = [ ...
    1 2; 1 3; % Uncertainty split
    2 4; 4 5; 4 6; % Extreme uncertainty
    5 7; 5 8; % Probability weighting effects
    6 9; 6 10; % Extrapolative effects
    3 11; 11 12; 11 13; % Mild uncertainty
    12 14; 12 15; % Probability weighting effects (mild)
    13 16; 13 17 % Extrapolative effects (mild)
];

% Create Graph
G = digraph(edges(:,1), edges(:,2), [], nodes);

% Plot Graph
figure;
h = plot(G, 'Layout', 'layered', 'NodeLabel', nodes, 'EdgeLabel', ...
    {'High', 'Low', '', 'Yes', 'No', 'Underweight Risk', 'Overweight Risk', ...
    'Extrapolate Trend', 'Hesitate to Act', '', 'Yes', 'No', 'Slow to Adjust', ...
    'Overprotect or Overcommit', 'Extrapolate Small Moves', 'Fail to See Inflection'});

% Improve Appearance
h.NodeColor = 'cyan';
h.MarkerSize = 8;
h.EdgeColor = [0 0 0];
h.LineWidth = 1.5;
title('Investor Decision Tree: Overreaction vs. Underreaction');

% -----------------------------------------------------------------------
% ------ Montecarlo of Stochastic Dominance -----------------------------
%________________________________________________________________________

clear; clc; rng(42);  % Set seed for reproducibility  

% Simulation Parameters  
N = 10000;   % Number of Monte Carlo simulations  
T = 50;      % Time periods  
mu = 0.05;   % Expected market return  
sigma_extreme = 0.4; % High volatility (extreme uncertainty)  
sigma_mild = 0.1;    % Low volatility (mild uncertainty)  

% Investor Parameters  
lambda = 0.7; % Cognitive dissonance (belief rigidity factor)  
theta = 0.5;  % Extrapolation coefficient (momentum sensitivity)  
gamma = 0.6;  % Probability weighting parameter (Tversky & Kahneman)  

% Generate Market Returns  
returns_extreme = mu + sigma_extreme * randn(N, T);  
returns_mild = mu + sigma_mild * randn(N, T);  

% Cognitive Dissonance (Extreme Uncertainty)  
prior_belief = mu;  
belief_rigid = lambda * prior_belief + (1 - lambda) * mean(returns_extreme, 2);  
w_p = @(p) (p.^gamma) ./ (p.^gamma + (1 - p).^gamma).^(1/gamma);  
prob_weighted_returns = w_p(normcdf(returns_extreme));  
wealth_cognitive = cumprod(1 + prob_weighted_returns, 2);  

% Extrapolative Beliefs (Mild Uncertainty)  
trend_adj_returns = mu + theta * (returns_mild - mu);  
wealth_extrapolative = cumprod(1 + trend_adj_returns, 2);  

% Expected Utility (CRRA Utility Function)  
alpha = 0.5; % Risk aversion coefficient  
U = @(x) (x.^(1-alpha) - 1) / (1-alpha);  
EU_cognitive = mean(U(wealth_cognitive(:, end)));  
EU_extrapolative = mean(U(wealth_extrapolative(:, end)));  

% Plot Monte Carlo Paths  
figure; hold on;  
plot(mean(wealth_cognitive, 1), 'r', 'LineWidth', 2);  
plot(mean(wealth_extrapolative, 1), 'b', 'LineWidth', 2);  
legend('Cognitive Dissonance (Extreme Uncertainty)', 'Extrapolative Beliefs (Mild Uncertainty)');  
xlabel('Time'); ylabel('Wealth');  
title('Monte Carlo Simulation of Investor Strategies');  
grid on;  

% Display Results  
fprintf('Expected Utility (Cognitive Dissonance, Extreme Uncertainty): %.4f\n', EU_cognitive);  
fprintf('Expected Utility (Extrapolative Beliefs, Mild Uncertainty): %.4f\n', EU_extrapolative);  
